#include <iostream>
using std::cout;
using std::endl;
//引用作为函数的参数
//这里面没有起作用的原因在于形参和实参结合的时候，
//进行了一次值的拷贝操作
//给swap里面的a和b进行了初始化操作，初始化的值分别是10和20
//但是这个10和20和test里面的a和b没有任何关系
void swap(int a, int b){
    cout << "swap::" << &a << ":" << &b << endl;
    int temp = a;
    a = b;
    b = temp;
}

void swap2(int * p1, int * p2){
    cout << "swap2:" << p1 << ":" << p2 << endl;//显示存储的地址
    cout << "swap2:" << &p1 << ":" << &p2 << endl;//对地址变量进行取地址
    int temp = *p1;
    *p1 = *p2;
    *p2 = temp;
}

void swap3(int & ref1, int & ref2){
    cout << "swap3:" << &ref1 << ":" << &ref2 << endl;
    int temp = ref1;
    ref1 = ref2;
    ref2 = temp;
}

void test(){
    int a = 10, b = 20;
    cout << &a << ":" << &b << endl;
    cout << a << ";" << b << endl;
    swap(a, b);
    cout << a << ";" << b << endl;
}

void test2(){
    int a = 10, b = 20;
    cout << &a << ":" << &b << endl;
    cout << a << ";" << b << endl;
    swap2(&a, &b);
    cout << a << ";" << b << endl;
}

void test3(){
    int a = 10, b = 20;
    cout << &a << ":" << &b << endl;
    cout << a << ";" << b << endl;
    swap3(a, b);
    cout << a << ";" << b << endl;
}

int main()
{
    test3();
    return 0;
}

